#!/bin/bash

# 捕获SIGTERM信号并退出
trap "exit" SIGTERM

category="CPU"
log='/tmp/stress-cpu.log'
err='/tmp/stress-cpu.err'
cycle=${1:-'600'}
resultcode=0
resultmsg=""

start_time=$(date +%s)

cpus=`lscpu |grep 'CPU(s):' |head -1 |awk -F':' '{print $2}' |awk '$1=$1'`


>$log
echo "cycle=$cycle,cpus=$cpus" >$log

stress-ng --cpu $cpus --timeout $cycle -v --log-file $log 2> $err &
stress_pid=$!  # 获取 stress-ng 的进程ID

# 等待直到 stress-ng 结束或接收到SIGTERM信号
wait $stress_pid

if [ $? -ne 0 ];then
	resultcode="1"
	resultmsg=`cat $err`
fi

end_time=$(date +%s)

run_time=$((end_time - start_time))

echo "category=${category};resultcode=${resultcode};resultmsg=${resultmsg};runtime=${run_time}"
